package com.yzf.controller;

import com.yzf.util.RedisTool;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.security.Principal;

@Controller
public class UserController {

	private Logger logger = LoggerFactory.getLogger(UserController.class);

	@Autowired
	private RedisTool redisTool;
	
	@RequestMapping("/demo")
	@PreAuthorize("hasAuthority('BookAdd')")
	// 页面使用 <span sec:authorize="hasAuthority('BookAdd')">
	public void selectUserById(){
		
	}
	
	/**
	 * 首页
	 * @return
	 */
	@RequestMapping({"/","/index"})
	public String index(){
		System.out.println("index-----------");
		return "/index.html";
	}
	
	/**
	 * 跳转到登录
	 * @return
	 */
	@GetMapping("/login")
	@ApiOperation(value="登录", notes="登录")
	public String toLogin(){
		System.out.println("跳转到登录-----------");
		return "login.html";
	}
	
	/**
	 * 登录成功
	 * @return
	 */
	@RequestMapping("/loginSuccess")
	public ModelAndView login(String password ,String username){
//		List<Object> menus=redisTool.lGet("menuList",0,-1);
//		StringBuilder sss=new StringBuilder();
//		for (Object menu:menus) {
//			Menu me=(Menu)menu;
//			sss.append(me.getMenuName()+",");
//		}
//		System.out.print("登陆成功，该用户获取到的菜单权限有："+sss);
		ModelAndView modelAndView =new ModelAndView("redirect:/pages/admin.html");
		System.out.println("登录-----------"+username+":"+password);
		modelAndView.addObject("username", username);
//		modelAndView.addObject("menuList",menus);
		return modelAndView;
	}
	
	/**
	 * 查询用户
	 * @return
	 */
	@PreAuthorize("hasAnyRole('ROLE_SELECT_USER')")
	@RequestMapping("/selectUser")
	@ResponseBody
	public boolean selectUser(@AuthenticationPrincipal Principal principal){
		System.out.println("selectUser-----------"+principal.getName());
		return true;
	}
	
	/**
	 * 添加用户
	 * @return
	 */
	@PreAuthorize("hasAnyRole('ROLE_INSERT_USER')")
	@RequestMapping("/insertUser")
	@ResponseBody
	public boolean insertUser(){
		System.out.println("insertUser-----------");
		return true;
	}
	
	/**
	 * 更新用户
	 * @return
	 */
	@PreAuthorize("hasAnyRole('ROLE_UPDATE_USER')")
	@RequestMapping("/updateUser")
	@ResponseBody
	public boolean updateUser(){
		System.out.println("updateUser-----------");
		return true;
	}
	
	/**
	 * 删除用户
	 * @return
	 */
	@PreAuthorize("hasRole('ROLE_DELETE_USER')")
	@RequestMapping("/deleteUser")
	@ResponseBody
	public boolean deleteUser(){
		System.out.println("deleteUser-----------");
		return true;
	}
	
}
